<template>
  <el-dialog
    class="afterInvest-add-open custom-dialog"
    width="60%"
    :visible.sync="visible"
    @close="closeHandle"
    :append-to-body="appendToBody"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <div slot="title" class="title">
      {{ title }}
    </div>
    <el-form
      ref="searchFrom1"
      :inline="true"
      label-width="80px"
      :model="searchFrom1"
      style="margin-top: 10px"
    >
      <el-form-item label="" prop="">
        <el-select
          v-model="allStrategyType"
          @change="selAllStrategyTypeHandle"
          placeholder="请选择"
          size="small"
          style="width: 120px"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            :disabled="item.disabled"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-if="allStrategyType == 0" label="所属投顾" prop="investName">
        <el-input
          v-model="searchFrom1.investName"
          clearable
          size="small"
          placeholder="请输入所属投顾名称"
          style="width: 260px"
          :disabled="noInvestName"
          @change="searchList()"
        ></el-input>
      </el-form-item>
      <el-form-item v-if="allStrategyType == 0" label="策略名称" prop="strategyName">
        <el-input
          v-model="searchFrom1.strategyName"
          clearable
          size="small"
          placeholder="请输入策略名称"
          style="width: 260px"
          @change="searchList()"
        ></el-input>
      </el-form-item>
      <el-form-item
        v-if="allStrategyType == 1"
        label="投资名称"
        prop="classfyName"
      >
        <el-input
          v-model="searchFrom1.investName"
          clearable
          size="small"
          placeholder="请输入投资名称"
          style="width: 260px"
          @change="searchList()"
        ></el-input>
      </el-form-item>
      <el-form-item v-if="allStrategyType == 1" label="" prop="investType">
        <el-select
          style="width: 260px;"
          v-model="searchFrom1.investType"
          size="small"
          placeholder="请选择投资形式"
          @keyup.enter.native="searchList"
          clearable
        >
          <el-option
            v-for="item in invesTypeOption"
            :key="item.id"
            :label="item.name"
            :value="item.id"
            :disabled="item.disabled"
          >
            {{ item.label }}
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-if="allStrategyType == 2" label="" prop="investName">
        <el-input v-model="searchFrom1.investName" size="small" @keyup.enter.native="searchList" clearable placeholder="请输入子投资名称" style="width: 200px;"></el-input>
      </el-form-item>
      <el-form-item v-if="allStrategyType == 2" label="" prop="investStatus">
        <el-select v-model="searchFrom1.investStatus" size="small" @keyup.enter.native="searchList" clearable placeholder="投资状态" style="width: 200px;">
          <el-option
            v-for="item in statusList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item style="margin-left: 20px">
        <el-button size="small" type="primary" @click="searchList()">查询</el-button>
        <el-button size="small" @click="resetSearchFrom1()">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="filex">
      <el-table
        v-if="allStrategyType == 0"
        ref="policyTable"
        :key="policyTable_key"
        v-loading="listLoading"
        :data="list"
        fit
        size="mini"
        :row-class-name="tableRowClassName"
        @row-click="submitHandle"
        @row-dblclick="dblclickHandle"
        :header-cell-style="{ background: '#F5F7FA', color: '#333333' }"
        style="width: 100%"
        max-height="515"
      >
        <el-table-column
          v-if="type === 'id'"
          type="index"
          label="编号"
          align="center"
          width="50"
          fixed
        >
          <template slot-scope="scope">
            <span>{{ scope.$index }}</span>
          </template>
        </el-table-column>
        <el-table-column
          v-else
          type="index"
          label=""
          align="center"
          width="50"
          fixed
        >
          <template slot-scope="scope">
            <span v-if="type === 'id'">{{ scope.$index }}</span>
            <el-radio
              v-else-if="type === 'radio'"
              v-model="radio"
              :label="scope.row.strategyInfoVo.strategyId"
              :disabled="
                scope.row.strategyInfoVo
                  ? !scope.row.strategyInfoVo.hasNetWorthStatus
                  : false
              "
            >
              {{ "" }}
            </el-radio>
            <el-checkbox
              v-else
              v-model="checkArr"
              :label="scope.row.strategyInfoVo.strategyId"
              :disabled="
                scope.row.strategyInfoVo
                  ? !scope.row.strategyInfoVo.hasNetWorthStatus
                  : false
              "
            >
              {{ "" }}
            </el-checkbox>
            <div class="checkhidden"></div>
          </template>
        </el-table-column>
        <el-table-column
          prop="strategyInfoVo.strategyName"
          label="策略名称"
          min-width="200"
          fixed
        >
          <template slot-scope="{ row }">
            <span
              v-if="
                !nolink &&
                row &&
                row.strategyInfoVo &&
                row.strategyInfoVo.strategyId
              "
              style="color: #4f95dd; cursor: pointer"
              v-strategy="{ id: row.strategyInfoVo.strategyId, userId }"
              >{{ row.strategyInfoVo.strategyName }}</span
            >
            <span v-else-if="nolink">
              【{{row.strategyInfoVo.strategyPool?'公有':'私有'}}】{{ row.strategyInfoVo.strategyName }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          prop="strategyInfoVo.investName"
          label="所属投顾"
          min-width="140"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="strategyInfoVo.strategyMarket"
          label="投资市场"
          min-width="100"
        ></el-table-column>
        <el-table-column
          prop="strategyInfoVo.strategyStyle"
          label="投资风格"
          width="80"
        ></el-table-column>
        <el-table-column
          prop="strategyInfoVo.strategyMethod"
          label="投资方法"
          min-width="100"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="strategyInfoVo.strategySubject"
          label="投资标的"
          min-width="100"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="netWorthDetialBean.networthTime"
          label="净值时间"
          sortable
          align="center"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="netWorthDetialBean.unitNetworth"
          label="单位净值"
          sortable
          align="center"
          width="100"
        ></el-table-column>
      </el-table>
      <el-table
        v-if="allStrategyType == 1"
        ref="investListTable"
        :key="investListTable_key"
        v-loading="listLoading"
        :data="list"
        stripe
        fit
        size="mini"
        @row-click="submitHandle"
        :header-cell-style="{ color: '#333' }"
        style="width: 100%;"
        max-height="515"
      >
        <el-table-column
          v-if="type === 'id'"
          type="index"
          label="编号"
          align="center"
          width="50"
          fixed
        >
          <template slot-scope="scope">
            <span>{{ scope.$index }}</span>
          </template>
        </el-table-column>
        <el-table-column
          v-else
          type="index"
          label=""
          align="center"
          width="50"
          fixed
        >
          <template slot-scope="scope">
            <span v-if="type === 'id'">{{ scope.$index }}</span>
            <el-radio
              v-else-if="type === 'radio'"
              v-model="radio"
              :label="scope.row.strategyInfoVo.strategyId"
              :disabled="!scope.row.hasNetWorth"
            >
              {{ "" }}
            </el-radio>
            <el-checkbox
              v-else
              v-model="checkArr"
              :label="scope.row.strategyInfoVo.strategyId"
              :disabled="!scope.row.hasNetWorth"
            >
              {{ "" }}
            </el-checkbox>
            <div class="checkhidden"></div>
          </template>
        </el-table-column>
        <el-table-column
          prop="investName"
         
          label="投资名称"
          min-width="150"
          show-overflow-tooltip
          fixed
        ></el-table-column>
        <el-table-column prop="investType" label="投资形式" min-width="90">
          <template slot-scope="{ row }">
            <div v-for="item in invesTypeOption" :key="item.id">
              <span v-if="row.investType === item.id">
                {{ item.label }}
              </span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="currentEquity"
         
          label="投资金额(元)"
          min-width="140"
        ></el-table-column>
        <el-table-column
          prop="incomeRate"
         
          label="收益率"
          min-width="120"
          show-overflow-tooltip
        >
          <template slot-scope="{ row }">
            <span
              v-if="row.incomeRate"
              :class="row.incomeRate >= 0 ? 'positiveFc' : 'negativeFc'"
            >
              {{ row.incomeRate }}%
            </span>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="investStarttime"
         
          label="投资时间"
          min-width="100"
          show-overflow-tooltip
        ></el-table-column>
      </el-table>
      <el-table
        v-if="allStrategyType == 2"
        ref="multipleTable"
        v-loading="listLoading"
        :data="list"
        stripe
        fit
        size="mini"
        @row-click="submitHandle"
        :header-cell-style="{color:'#333'}"
        style="width: 100%;"
        max-height="515"
      >
        <el-table-column
          v-if="type === 'id'"
          type="index"
          label="编号"
          align="center"
          width="50"
          fixed
        >
          <template slot-scope="scope">
            <span>{{ scope.$index }}</span>
          </template>
        </el-table-column>
        <el-table-column
          v-else
          type="index"
          label=""
          align="center"
          width="50"
          fixed
        >
          <template slot-scope="scope">
            <span v-if="type === 'id'">{{ scope.$index }}</span>
            <el-radio
              v-else-if="type === 'radio'"
              v-model="radio"
              :label="scope.row.strategyInfoVo.strategyId"
              :disabled="!scope.row.hasNetWorth"
            >
              {{ "" }}
            </el-radio>
            <el-checkbox
              v-else
              v-model="checkArr"
              :label="scope.row.strategyInfoVo.strategyId"
              :disabled="!scope.row.hasNetWorth"
            >
              {{ "" }}
            </el-checkbox>
            <div class="checkhidden"></div>
          </template>
        </el-table-column>
        <el-table-column prop="investName" label="子投资名称" align="left" min-width="150" fixed show-overflow-tooltip>
          <template slot-scope="{row}">
            <span>{{ row.investName }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="currentEquity" label="资产金额(元)" min-width="120" align="center"></el-table-column>
        <el-table-column prop="incomeRate" label="收益率" min-width="120" show-overflow-tooltip align="center">
          <template slot-scope="{row}">
            <span v-if="row.incomeRate||row.incomeRate!==null" :class="row.incomeRate>=0?'positiveFc':'negativeFc'">{{ row.incomeRate }}%</span>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column prop="investStarttime" label="投资时间" min-width="100" show-overflow-tooltip align="center"></el-table-column>
        <el-table-column prop="investStatusName" label="投资状态" min-width="100" show-overflow-tooltip align="center"></el-table-column>
      </el-table>
      <div class="list" v-if="type === 'check'">
        <div class="list-title">
          <span>已勾选</span>
          <span>({{ checkList.length }})</span>
        </div>
        <div style="height: 468px; overflow-y: scroll">
          <div
            class="list-item"
            v-for="(item, index) in checkList"
            :key="index"
          >
            <span class="list-item-content">
              {{ item.strategyInfoVo.strategyName }}
            </span>
            <i class="el-icon-close close" @click="delList(index)"></i>
          </div>
        </div>
      </div>
    </div>

    <div>
      <el-button
        v-if="type !== 'id'"
        style="float: right; margin-top: 15px; width: 160px; font-size: 12px"
        type="primary"
        size="small"
        @click="submitCheck()"
      >
        确认
      </el-button>
      <pagination
        style="text-algin: left"
        v-show="total > 0"
        :total="total"
        :layout="'total, sizes, prev, pager, next'"
        :page.sync="listQuery.pageCurrent"
        :limit.sync="listQuery.limit"
        @pagination="paginationHandle"
      />
    </div>
  </el-dialog>
</template>

<script>
import { fetchList } from "@/api/policyManage";
import Pagination from "@/components/Pagination";
import { queryInvestInfoByPage } from '@/api/afterInvest'

export default {
  props: {
    userId: String | Number,
    title: { type: String, default: "更改策略（单选）" },
    type: { type: String, default: "id" },
    nolink: {
      type: Boolean,
      default: false
    },
    appendToBody: {
      type: Boolean,
      default: false
    }
  },
  components: { Pagination },
  data() {
    return {
      noInvestName: false,
      visible: false,
      listLoading: false,
      total: 0,
      listQuery: {
        pageCurrent: 1,
        limit: 20
      },
      searchFrom1: {
        investName: "",
        strategyName: "",
        searchType: 1
      },
      list: [],
      checkList: [],
      radioList: [],
      radio: "",
      checkArr: [],

      investListTable_key: 1,
      policyTable_key: 1,
      allStrategyType: 0,
      options: [
        {
          value: 0,
          label: "策略"
        },
        {
          value: 1,
          label: "投资"
        },
        {
          value: 2,
          label: "子投资"
        }
      ],
      invesTypeOption: [
        {
          id: 1,
          name: '投资形式: MOM',
          label: 'MOM',
        },
        {
          id: 2,
          name: '投资形式: FOF',
          label: 'FOF',
        },
        {
          id: 3,
          name: '投资形式：股票专户',
          label: '股票专户',
        },
        {
          id: 4,
          name: '投资形式：期货专户',
          label: '期货专户',
        },
        {
          id: 5,
          name: '投资形式：基金/信托产品',
          label: '基金/信托产品',
        },
        {
          id: 6,
          name: '投资形式：其他',
          label: '其他'
        },
      ],
      statusList: [
        {
          id: 1,
          name: '未开始',
        },
        {
          id: 2,
          name: '进行中',
        },
        {
          id: 3,
          name: '已终止',
        },
      ],
    };
  },
  methods: {
    paginationHandle() {
      const type = this.allStrategyType;
      if (type == 0) {
        this.getPolicyList();
      } else if (type == 1) {
        this.queryInvestInfoByPage(1);
      } else if (type == 2) {
        this.queryInvestInfoByPage(2);
      }
    },
    selAllStrategyTypeHandle(type) {
      this.listQuery.pageCurrent = 1;
      this.$refs["searchFrom1"].resetFields();
      if (type == 0) {
        this.getPolicyList();
      } else if (type == 1) {
        this.queryInvestInfoByPage(1)
      } else if (type == 2) {
        this.queryInvestInfoByPage(2);
      }
    },
    
    queryInvestInfoByPage(type) {
      this.listLoading = true
      queryInvestInfoByPage({
        ...this.searchFrom1,
        ...this.listQuery,
        orderBy: this.orderInfo,
        investManageType: type,
        userId: this.userId,
      }).then((res) => {
        this.listLoading = false
        this.list = res.body.list.map((items, index) => {
          return {
            ...items,
            id: index,
            hasNetWorth: items.hasNetworth,
            strategyInfoVo: {
              ...items,
              strategyId: items.investId,
              strategyName: items.investName,
              makeTime: items.investStarttime,
            }
          }
        });
        this.total = res.body.total || 0;
        console.log('投资列表：', res, this.list)

        this.investListTable_key++
      })
    },

    tableRowClassName({ row, rowIndex }) {
      return "list-row";
    },
    closeHandle() {
      this.visible = false;
    },
    // 初始化
    init() {
      this.radio = "";
      this.visible = true;
      this.checkArr = [];
      this.checkList = [];
      this.listQuery = {
        pageCurrent: 1,
        limit: 20
      };
      this.$nextTick(() => {
        this.$refs["searchFrom1"].resetFields();
        // this.$refs.policyTable.clearSelection();
        // this.getPolicyList();
        if (this.allStrategyType == 0) {
          this.getPolicyList()
        } else if (this.allStrategyType == 1) {
          this.queryInvestInfoByPage(1)
        } else if (this.allStrategyType == 2) {
          this.queryInvestInfoByPage(2);
        }
      });
    },
    searchList() {
      this.listQuery.pageCurrent = 1
      if (this.allStrategyType == 0) {
        this.getPolicyList()
      } else if (this.allStrategyType == 1) {
        this.queryInvestInfoByPage(1)
      } else if (this.allStrategyType == 2) {
        this.queryInvestInfoByPage(2);
      }
    },
    // 策略列表
    getPolicyList() {
      return new Promise((resolve) => {
        this.listLoading = true;
        fetchList({
          userId: this.userId,
          ...this.searchFrom1,
          ...this.listQuery
        }).then((res) => {
          this.listLoading = false;
          this.list = res.body.map((items, index) => {
            items.id = index;
            return items;
          });
          console.log("策略列表", this.list);
          this.total = res.count || 0;
          resolve(true);
          this.policyTable_key++;
        });
      });
    },
    resetSearchFrom1() {
      this.$refs["searchFrom1"].resetFields();
      this.searchFrom1 = {
        investName: "",
        strategyName: "",
        searchType: 1
      }
      if (this.allStrategyType == 0) {
        this.getPolicyList()
      } else if (this.allStrategyType == 1) {
        this.queryInvestInfoByPage(1)
      } else if (this.allStrategyType == 2) {
        this.queryInvestInfoByPage(2);
      }
    },
    // 单选双击选中
    dblclickHandle(row) {
      if (this.type !== "check") {
        this.$emit("selctInvestName", row.strategyInfoVo);
        this.visible = false;
      }
    },
    // 行选中
    submitHandle(row, col) {
      if(this.allStrategyType==0) {
        if (!row.strategyInfoVo.hasNetWorthStatus) {
          this.$message({
            message: "该产品净值不存在或者少于2条",
            warning: "warning"
          });
          return;
        }
      } else {
        if (!row.hasNetWorth) {
          this.$message({
            message: "该产品净值不存在或者少于2条",
            warning: "warning"
          });
          return;
        }
      }
      if (this.type === "id") {
        this.$emit("selctInvestName", row.strategyInfoVo);
        this.visible = false;
      } else if (this.type === "radio") {
        this.radio = row.strategyInfoVo.strategyId;
        this.radioList = row;
      } else if (this.type === "check") {
        if (this.checkArr.indexOf(row.strategyInfoVo.strategyId) === -1) {
          this.checkArr.push(row.strategyInfoVo.strategyId);
          this.checkList.push(row);
        } else {
          for (var i = 0; i < this.checkArr.length; i++) {
            if (this.checkArr[i] === row.strategyInfoVo.strategyId) {
              this.checkArr.splice(i, 1);
              this.checkList.splice(i, 1);
            }
          }
        }
      }
    },
    delList(i) {
      this.checkArr.splice(i, 1);
      this.checkList.splice(i, 1);
    },
    // 多选
    handleSelectionChange(rows) {
      this.checkList = rows.map((item) => {
        return item;
      });
    },
    // 确认
    submitCheck() {
      console.log(this.radioList);
      if (this.type === "radio" && this.radioList.id !== undefined) {
        this.$emit("selctInvestName", this.radioList.strategyInfoVo);
        this.visible = false;
      } else if (this.type === "check" && this.checkList.length > 0) {
        this.$emit("selCheckctInvestName", this.checkList);
        this.visible = false;
      } else {
        this.$message({
          message: "请选择策略",
          type: "info",
          duration: 2 * 1000
        });
      }
    }
  }
};
</script>
<style lang="scss">
.list-row {
  cursor: pointer;
}
</style>
<style lang="scss" scoped>
.afterInvest-add-open {
  ::v-deep .el-dialog__body {
    min-height: 600px;
    padding-top: 10px;
    padding-bottom: 10px;
  }
  ::v-deep .pagination-container {
    padding: 15px 0;
    text-align: left;
  }
  .title {
    line-height: 53px;
    background: #d4dfef;
    padding-left: 20px;
    color: #333333;
    font-weight: bold;
    font-size: 18px;
  }
  .title2 {
    margin-top: 15px;
  }
  .head {
    border: 1px solid #ddd;
    ::v-deep .el-form {
      margin-top: 15px;
    }
  }
  .filex {
    display: flex;
    justify-content: space-between;
    .list {
      margin-left: 15px;
      width: 280px;
      min-height: 200px;

      &-title {
        background: #f5f7fa;
        color: #333;
        font-weight: bold;
        height: 47px;
        padding-left: 16px;
        line-height: 36px;
      }
      &-item {
        background: #f5f7fa;
        margin-top: 10px;
        font-size: 14px;
        padding-left: 10px;
        line-height: 30px;
        &:hover {
          color: #1890ff;
        }
        &-content {
          width: 210px;
          display: inline-block;
        }
      }
      .close {
        padding: 0 10px;
        line-height: 30px;
        cursor: pointer;
      }
    }
  }
  .table-content {
    border: 1px solid #ddd;
    padding: 5px;
    font-size: 12px;
    font-weight: bold;
    // margin-top: 15px;
  }
  .checkhidden {
    display: inline-block;
    width: 80px;
    height: 34px;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 999;
  }
  ::v-deep .el-table__fixed {
    height: 100% !important; //设置高优先，以覆盖内联样式
  }
  ::v-deep .el-table__fixed-right {
    height: 100% !important; //设置高优先，以覆盖内联样式
  }
}
</style>
